package ipmsg.gui;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.TransferHandler;

import ipmsg.etc.GlobalVar;












public class MyTransferHandler
  extends TransferHandler
{
  private JList jlist;
  
  public MyTransferHandler(JList jlist)
  {
    this.jlist = jlist;
  }
  



  public boolean canImport(TransferHandler.TransferSupport support)
  {
    if (((GlobalVar.OS == '\001') && (!support.isDataFlavorSupported(DataFlavor.javaFileListFlavor))) || ((GlobalVar.OS == 0) && (!support.isDataFlavorSupported(DataFlavor.stringFlavor))))
    {



      return false;
    }
    return true;
  }
  




  public boolean importData(TransferHandler.TransferSupport support)
  {
    if (!canImport(support)) {
      return false;
    }
    List list = null;
    
    Transferable tr = support.getTransferable();
    
    DefaultListModel listModel;
    if (this.jlist.getModel().getSize() == 0) {
      listModel = new DefaultListModel();
    } else {
      listModel = (DefaultListModel)this.jlist.getModel();
    }
    

    if (support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
      try {
        list = (List)tr.getTransferData(DataFlavor.javaFileListFlavor);
      }
      catch (UnsupportedFlavorException e) {
        e.printStackTrace();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
      Iterator iterator = list.iterator();
      while (iterator.hasNext()) {
        File f = (File)iterator.next();
        
        if (!listModel.contains(f.getPath())) {
          listModel.addElement(f.getPath());
        }
      }
    }
    if (support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
      String listname = null;
      try {
        listname = tr.getTransferData(DataFlavor.stringFlavor).toString();
      }
      catch (UnsupportedFlavorException e) {
        e.printStackTrace();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
      list = new ArrayList(Arrays.asList(listname.split("\r\n")));
      Iterator iterator = list.iterator();
      while (iterator.hasNext()) {
        File f = null;
        try {
          f = new File(new URI((String)iterator.next()));
          

          if (!listModel.contains(f.getPath())) {
            listModel.addElement(f.getPath());
          }
          

        }
        catch (NullPointerException e) {}catch (URISyntaxException ex) {}catch (IllegalArgumentException e) {}
      }
    }
    else
    {
      return false;
    }
    Iterator iterator;
    this.jlist.setModel(listModel);
    if (this.jlist.getModel().getSize() > 0) {
      this.jlist.setSelectedIndex(0);
    }
    return true;
  }
}
